/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: DayController.java 46 2012-09-19 09:10:23Z zhuxiaopeng $
 * $Revision: 46 $
 * $Date: 2012-09-19 17:10:23 +0800 (三, 2012-09-19) $
 * $Author: zhuxiaopeng $
 */
package daily.web.day;

import static zhu.framework.web.WebUtility.*;

import java.util.*;

import javax.ejb.*;
import javax.enterprise.context.*;
import javax.inject.*;

import zhu.framework.service.*;
import zhu.framework.web.*;
import zhu.framework.web.jsf.*;
import daily.entity.*;
import daily.service.*;
import daily.web.SessionBean;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 46 $ - $Date: 2012-09-19 17:10:23 +0800 (三, 2012-09-19) $
 */
@Named
@RequestScoped
public class DayController extends ConversationalController {

	private enum ActionOutCome {
		EDIT, SAVE, CANCEL
	}

	public String editDaily() {
		return ActionOutCome.EDIT.toString();
	}

	public String saveDaily() {
		try {
			dailyService.saveDaily(dayConversation.getSelectDaily());
		} catch (final BusinessException e) {
			getWebRequestContext().addMessages(WebMessage.valueOf(e.getMessages()));
			return null;
		}
		dayConversation.setDailyList(null);
		return ActionOutCome.SAVE.toString();
	}

	public String cancel() {
		return ActionOutCome.CANCEL.toString();
	}

	public List<DailyData> getDailyList() {
		List<DailyData> dailyList = dayConversation.getDailyList();
		if (dailyList == null) {
			final Staff loginStaff = getServiceRequestContext().getLoginStaff();
			dailyList = dailyService.getDailyByDay(loginStaff, sessionBean.getStaffList());
			dayConversation.setDailyList(dailyList);
		}
		return dailyList;
	}

	@Inject
	private DayConversation dayConversation;

	@EJB
	private DailyServiceImpl dailyService;

	@Inject
	private SessionBean sessionBean;
}
